<template>
  <div class="layout-container">
    <div class="layout-header">
      <design-header :zoom="zoom" @save="save" @clear="clear"></design-header>
    </div>
    <div class="layout-content">
      <!-- 功能区 -->
      <div class="layout-aside-left">
        <design-panel :list="complexList" />
      </div>
      <div class="layout-main">
        <div class="clear-rluer"></div>
        <!-- 右键菜单 -->
        <design-menu :config="contextMenu" />
        <div
          ref="screen"
          id="screen"
          class="desgin-canvas"
          v-resize="handleResize"
          @mousedown="mousedownDesign"
        >
          <!-- 顶部标尺数字绘制 -->
          <div class="v-ruler-canvas-content">
            <canvas ref="v-ruler-canvas" :width="criterion.width" height="20" />
          </div>
          <!-- 画布区域 -->
          <div
            class="design-content"
            ref="design"
            id="design"
            :style="designStyle(layoutConfig)"
            @drop="handleDrop"
            @dragover="handleDragOver"
          >
            <DesignView
              :complex-list="complexList"
              :complex-item="complexItem"
              :zoom="layoutConfig.scale"
            />
          </div>
          <!-- 侧边标尺数字绘制 -->
          <div class="h-ruler-canvas-content">
            <canvas
              ref="h-ruler-canvas"
              :height="criterion.height"
              width="20"
            />
          </div>
        </div>

        <!-- 默认数据配置 -->
        <div
          class="data-source-dialog"
          :style="{
            bottom: sourceDialog ? '0px' : '-580px',
          }"
        >
          <div class="title" @click="editorClose">
            <span>基础数据配置</span>
            <svg-icon class="title-icon" icon="down" />
          </div>
          <div class="content">
            <div class="json-container">
              <b-code-editor
                ref="editor"
                v-model="requestData"
                :indent-unit="4"
                theme="material-darker"
                :auto-format="true"
                width="100%"
                height="750px"
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 组件属性 -->
      <div class="layout-aside-right">
        <design-attribute :page="layoutConfig" />
      </div>
    </div>
  </div>
</template>

<script>
import DesignMenu from "./components/DesignMenu";
import DesignPanel from "./components/DesignPanel";
import DesignAttribute from "./components/DesignAttribute";
import DesignView from "./components/DesignView";
import DesignHeader from "./components/Header";
import { mapState } from "vuex";
export default {
  components: {
    DesignAttribute,
    DesignPanel,
    DesignView,
    DesignMenu,
    DesignHeader,
  },
  directives: {
    resize: {
      bind(e, binding) {
        let width = 0;
        let height = 0;
        const isReize = () => {
          let offsetWidth = e.offsetWidth;
          let offsetHeight = e.offsetHeight;
          if (width !== offsetWidth || height !== offsetHeight) {
            const value = {
              width: offsetWidth,
              height: offsetHeight,
            };
            binding.value(value);
          }
          width = offsetWidth;
          height = offsetHeight;
        };
        e.__vueSetInterval__ = setInterval(isReize, 300);
      },
      unbind(e) {
        clearInterval(e.__vueSetInterval__);
      },
    },
  },
  data() {
    return {
      index: 0,
      isSpace: false, // 按住空格
      sourceDialog: false, // 数据编辑器
      // 编辑器
      criterion: {
        width: 3000,
        height: 3000,
      },
      // 画布尺寸
      layoutConfig: {
        width: 1920, // 宽度
        height: 1080, // 高度
        left: 0,
        top: 0,
        scale: 0.5, // 缩放
        layout: {
          row: 4,
          column: 4,
          gutter: 6,
        },
      },
      complexItem: null,
      keyCode: {
        ctrl: false,
        space: false,
        plus: false,
        cut: false,
        delete: false,
      },
    };
  },
  computed: {
    ...mapState({
      complexList: (state) => state.design.complexData,
      contextMenu: (state) => state.design.contextMenu,
    }),
    // 编辑器数据
    requestData: {
      get() {
        const complex = this.$store.state.design.selectComplex;
        return complex && complex.config.data
          ? JSON.stringify(
              this.$store.state.design.selectComplex.config.data.value,
              null,
              2
            )
          : "";
      },
      set(val) {},
    },
    zoom() {
      return (this.layoutConfig.scale * 100).toFixed(0);
    },
  },
  created() {
    document.oncontextmenu = function () {
      return false;
    };
    // window.addEventListener("keyup", this.handleKeyup);
    // window.addEventListener("keydown", this.handleKeydown);
  },
  // beforeDestroy() {
  //   // 在组件生命周期结束的时候销毁。
  //   window.removeEventListener("keyup", this.handleKeyup, false);
  //   window.removeEventListener("keydown", this.handleKeydown, false);
  // },
  mounted() {
    this.setScreenCenter();
    this.drawRulerX();
    this.drawRulerY();
  },
  methods: {
    // 保存页面组件
    save() {
      console.log(this.layoutConfig);
      console.log(this.complexList);
      this.$store.dispatch("design/saveComplex", this.complexList);
      const pageConfig = {
        page: {
          name: this.$route.query.name,
          weidth: this.layoutConfig.width,
          height: this.layoutConfig.height,
        },
        complexList: this.complexList,
      };
      console.log(pageConfig);
    },
    // 清空画布
    clear() {
      this.$store.dispatch("design/clear");
    },
    // 关闭数据管理弹窗
    editorClose() {
      this.$refs.editor.formatCode();
      this.beforeClose();
      const data = this.$refs["editor"].getValue();

      this.sourceDialog = false;
    },
    beforeClose() {
      const data = this.$refs["editor"].getValue();
      if (this.sourceDialog) {
        try {
          this.$store.dispatch("design/setComplexDataValue", data);
        } catch (error) {
          console.log("当前JONS格式有错误");
        }
      }
    },
    // 样式处理
    designStyle(style) {
      return {
        width: `${style.width}px`,
        height: `${style.height}px`,
        transform: `translate(${style.left}px, ${style.top}px) scale(${style.scale}, ${style.scale})`,
      };
    },
    // 键盘按下事件
    handleKeydown() {
      document.onkeydown = (e) => {
        //事件对象兼容
        let el = e || window.event;
        let keyCode = el.keyCode;
        //关闭浏览器快捷键
        window.event.preventDefault();
        // 保存画布"ctrl and s"
        if (keyCode == 83 && el.ctrlKey) {
          this.save();
        }
        // 画布放大"ctrl and +"
        if ((el.ctrlKey && el.keyCode == 107) || el.keyCode == 187) {
          this.setScale("plus");
        }
        // 画布缩小"ctrl and -"
        if ((el.ctrlKey && el.keyCode == 109) || el.keyCode == 189) {
          this.setScale("cut");
        }
        if (keyCode == 32) {
          this.isSpace = true;
          document.getElementById("screen").style.cursor = "auto";
          console.log("空格");
        }
      };
    },
    // 键盘松开事件
    handleKeyup() {
      document.onkeyup = (e) => {
        //事件对象兼容
        let el = e || window.event || arguments.callee.caller.arguments[0];
        let keyCode = el.keyCode;
        if (keyCode == 32) {
          this.isSpace = true;
          document.getElementById("screen").style.cursor = "auto";
          console.log("空格");
        }
      };
    },
    // 画布放大缩小
    setScale(type) {
      let _scale = 0.05;
      if (type === "plus") {
        if (this.layoutConfig.scale < 1) {
          this.layoutConfig.scale += _scale;
        } else {
          this.layoutConfig.scale = 1;
        }
      }
      if (type === "cut") {
        if (this.layoutConfig.scale > 0.2) {
          this.layoutConfig.scale -= _scale;
        } else {
          this.layoutConfig.scale = 0.2;
        }
      }
    },
    // 鼠标按下事件
    mousedownDesign(e) {
      console.log("鼠标按下事件", e);
      this.beforeClose();
      this.sourceDialog = false; // 关闭数据框
    },
    // 监听编辑器窗口大小
    handleResize(e) {
      // 0.95 * 当前可编辑区域宽度
      this.layoutConfig.scale = (e.width * 0.95) / this.layoutConfig.width;
      const rounding = (number) => {
        const digits = number.toString().length;
        let num = 0;
        if (digits > 1) {
          const unitsDigit = parseInt(number.toString()[digits - 1]);
          num = number - unitsDigit;
        }
        return num;
      };
      const dom = document.getElementById("screen");
      const offsetWidth = e.width;
      const offsetHeight = e.height;
      const scrollLeft = (this.criterion.width - dom.offsetWidth) / 2;
      const scrollTop = (this.criterion.height - dom.offsetHeight) / 2;
      dom.scrollLeft = scrollLeft;
      dom.scrollTop = scrollTop;
      const centerX =
        scrollLeft + (offsetWidth / 2 - this.layoutConfig.width / 2);
      const centerY =
        scrollTop + (offsetHeight / 2 - this.layoutConfig.height / 2);
      this.layoutConfig.left = rounding(centerX);
      this.layoutConfig.top = rounding(centerY);
    },
    // 设置画布相对编辑器窗口居中
    setScreenCenter() {
      const rounding = (number) => {
        const digits = number.toString().length;
        let num = 0;
        if (digits > 1) {
          const unitsDigit = parseInt(number.toString()[digits - 1]);
          num = number - unitsDigit;
        }
        return num;
      };
      // 0.95 * 当前可编辑区域宽度
      const dom = document.getElementById("screen");
      const offsetWidth = dom.offsetWidth;
      const offsetHeight = dom.offsetHeight;
      const scrollLeft = (this.criterion.width - dom.offsetWidth) / 2;
      const scrollTop = (this.criterion.height - dom.offsetHeight) / 2;
      dom.scrollLeft = scrollLeft;
      dom.scrollTop = scrollTop;
      const centerX =
        scrollLeft + (offsetWidth / 2 - this.layoutConfig.width / 2);
      const centerY =
        scrollTop + (offsetHeight / 2 - this.layoutConfig.height / 2);
      this.layoutConfig.left = rounding(centerX);
      this.layoutConfig.top = rounding(centerY);
    },
    // 释放组件通过 store 添加
    handleDrop(e) {
      e.preventDefault();
      e.stopPropagation();
      const offset = {
        x: e.offsetX,
        y: e.offsetY,
      };
      console.log("鼠标释放");
      this.$store.dispatch("design/putComplex", offset);
    },
    // 拖拽元素
    handleDragOver(e) {
      e.preventDefault();
    },
    // 绘制横向刻度
    drawRulerX() {
      const zoom = this.layoutConfig.scale;
      const canvas = this.$refs["v-ruler-canvas"];
      const ctx = canvas.getContext("2d");
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "#bbb"; // 刻度颜色
      ctx.beginPath();
      // 画布居中
      let _width = this.layoutConfig.left;
      let y = 0;
      for (let i = 0; i <= this.criterion.width; i += 10) {
        const index = _width - i;
        let scale = Math.abs(index);
        let y = scale % 100 === 0 ? 12 : 6;
        if (index <= 0) {
          ctx.moveTo(scale + _width, y);
          ctx.lineTo(scale + _width, 0);
          ctx.stroke();
        }

        ctx.moveTo(i, y);
        ctx.lineTo(i, 0);
        ctx.stroke();

        if (scale % 100 === 0) {
          ctx.fillStyle = "#bbb";
          ctx.font = "5px";
          ctx.fillText((-1 * index).toString(), i + 4, 18);
        }
      }
    },
    // 绘制纵向刻度
    drawRulerY() {
      const canvas = this.$refs["h-ruler-canvas"];
      const ctx = canvas.getContext("2d");
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.lineWidth = 1;
      ctx.strokeStyle = "#bbb"; // 刻度颜色
      ctx.beginPath();
      // 画布居中
      let _height = this.layoutConfig.top;
      for (let i = 0; i < this.criterion.height; i += 10) {
        const index = _height - i;

        if (index <= 0) {
          let scale = Math.abs(index);
          let x = scale % 100 === 0 ? 12 : 6;
          ctx.moveTo(0, scale + _height);
          ctx.lineTo(x, scale + _height);
          ctx.stroke();
          ctx.closePath();
          ctx.save();

          ctx.beginPath();
          if (scale % 100 === 0) {
            ctx.translate(12, scale + _height + 4);
            ctx.rotate((90 * Math.PI) / 180);
            ctx.fillStyle = "#bbb";
            ctx.font = "5px";
            ctx.fillText(scale.toString(), 0, 0);
            ctx.closePath();
            ctx.restore();
          }
        }

        if (index > 0) {
          let scale = Math.abs(index);
          let x = scale % 100 === 0 ? 12 : 6;
          ctx.moveTo(0, i);
          ctx.lineTo(x, i);
          ctx.stroke();
          ctx.closePath();
          ctx.save();

          ctx.beginPath();
          if (scale % 100 === 0) {
            ctx.translate(12, i + 4);
            ctx.rotate((90 * Math.PI) / 180);
            ctx.fillStyle = "#bbb";
            ctx.font = "5px";
            ctx.fillText((-1 * index).toString(), 0, 0);
            ctx.closePath();
            ctx.restore();
          }
        }
      }
    },
  },
};
</script>

<style scoped lang="scss">
$bg-color: rgb(255, 255, 255);
$border-color: rgb(219 219 219);

.layout-container {
  width: 100%;
  height: 100vh;
  background: #f4f4f4;
  overflow: hidden;
}

.layout-header {
  position: relative;
  width: 100%;
  height: 48px;
  padding: 0px 16px;
  box-sizing: border-box;
  background: $bg-color;
  box-shadow: rgb(219 219 219) 0px 1px 0px;
  z-index: 1024;
}

.layout-content {
  width: 100%;
  height: calc(100% - 48px);
  display: flex;
  justify-content: space-between;

  .layout-aside-left {
    width: 244px;
    height: 100%;
    background: $bg-color;
    border-right: 1px solid $border-color;
  }

  .layout-main {
    position: relative;
    flex: 1;
    height: 100%;
    overflow: hidden;

    #screen {
      position: relative;
      width: 100%;
      height: 100%;
      overflow: auto;
    }

    .clear-rluer {
      position: absolute;
      width: 15px;
      height: 15px;
      background-color: rgba(22, 132, 252, 0.3);
      z-index: 2044;
      cursor: pointer;
      box-sizing: border-box;

      &:hover {
        cursor: pointer;
      }
    }

    .v-ruler-canvas-content {
      position: sticky;
      top: 0;
      height: 16px;
      z-index: 99;
    }

    .h-ruler-canvas-content {
      position: sticky;
      left: 0;
      width: 16px;
      z-index: 99;
    }

    .design-content {
      position: absolute;
      background-color: #fff;
      border: 1px solid #eee;
      transform-origin: center center;
      transition: all 0.28s ease;
    }
  }

  .layout-aside-right {
    width: 240px;
    height: 100%;
    background: $bg-color;
    border-left: 1px solid $border-color;
  }
}

.data-source-dialog {
  position: absolute;
  width: 100%;
  background: #f7f7f7;
  bottom: 0;
  z-index: 2024;
  box-sizing: border-box;
  transition: all 0.48s ease;
  overflow: hidden;

  .title {
    width: 100%;
    background: #363636;
    color: #ddd;
    box-shadow: 0 1px 0 #252626, 0 -1px 0 #252626;
    cursor: pointer;
    padding: 4px 6px;
    text-align: center;

    & span {
      float: left;
      font-size: 14px;
    }

    .title-icon {
      width: 16px;
      // color: #ddd;
    }
  }

  .content {
    width: 100%;
    height: 500px;

    .json-container {
      width: 100%;
      height: 100%;

      ::v-deep {
        .CodeMirror {
          position: relative;
          overflow: hidden;
          height: 500px !important;
          font-size: 16px;
        }
      }
    }
  }
}
</style>
